<?php
/**
 * Created by PhpStorm.
 * LICENSE: Apache 2.0
 * Date: 2019/3/7
 * Time: 下午9:13
 */

namespace app\portal\controller;


use app\portal\model\MessageModel;
use app\portal\validate\Messages;
use cmf\controller\HomeBaseController;
use think\Request;

class MessageController extends HomeBaseController
{
    /**
     * 提交留言
     * @param Request $request
     * @return void
     * @url /portal/message/postNewMessage
     * @method POST
     */
    public function postNewMessage(Request $request)
    {
        if(strtolower($request->method())!='post'){
            $this->error('非法请求');
        }
        $data = $request->post();
        $validate = new Messages();
        if (!$validate->check($data)) {
            $error = $validate->getError();
            $this->error($error);
        }
        $message = new MessageModel();
        $message->truename = $data['name'];
        $message->mobile = isset($data['mobile'])?intval($data['mobile']):'';
        $message->email = isset($data['email'])?$data['email']:'';
        $message->message = isset($data['message'])?$data['message']:'';
        $message->status = MessageModel::STATUS_UNREAD;
        $message->created_at = date('Y-m-d H:i:s',time());
        $message->updated_at = date('Y-m-d H:i:s',time());
        $message->save();
        $this->success();
    }
}